package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuFuliao;
import cn.hellochaos.generator.entity.CaipuTips;
import cn.hellochaos.generator.entity.CaipuZhuliao;
import cn.hellochaos.generator.mapper.CaipuZhuliaoMapper;
import cn.hellochaos.generator.service.CaipuZhuliaoService;
import cn.hellochaos.generator.utils.CheckObjectIsNullUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 主料 服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class CaipuZhuliaoServiceImpl extends ServiceImpl<CaipuZhuliaoMapper, CaipuZhuliao> implements CaipuZhuliaoService {

    @Autowired
    CaipuZhuliaoMapper caipuZhuliaoMapper;

    @Override
    public List<CaipuZhuliao> getCaipuBuzhouByCookBookId(Integer caipuBookId) {
        QueryWrapper<CaipuZhuliao> queryWrapper = new QueryWrapper<CaipuZhuliao>();
        queryWrapper.eq("id_cookbook",caipuBookId);
        List<CaipuZhuliao> caipuZhuliaos =caipuZhuliaoMapper.selectList(queryWrapper);

        if (CheckObjectIsNullUtils.objCheckIsNull(caipuZhuliaos)){
            return new ArrayList<CaipuZhuliao>();
        }

        return caipuZhuliaos;
    }

    @Override
    public Page<CaipuZhuliao> listCaipuZhuliaosByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询caipuZhuliao: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<CaipuZhuliao> queryWrapper =  new QueryWrapper<CaipuZhuliao>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<CaipuZhuliao> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询caipuZhuliao完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public CaipuZhuliao getCaipuZhuliaoById(int id) {
        log.info("正在查询caipuZhuliao中id为{}的数据",id);
        CaipuZhuliao caipuZhuliao = super.getById(id);
        log.info("查询id为{}的caipuZhuliao{}",id,(null == caipuZhuliao?"无结果":"成功"));
        return caipuZhuliao;
    }

    @Override
    public int insertCaipuZhuliao(CaipuZhuliao caipuZhuliao) {
        log.info("正在插入caipuZhuliao");
        if (super.save(caipuZhuliao)) {
            log.info("插入caipuZhuliao成功,id为{}",caipuZhuliao.getCaipuZhuliaoId());
            return caipuZhuliao.getCaipuZhuliaoId();
        } else {
            log.error("插入caipuZhuliao失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteCaipuZhuliaoById(int id) {
        log.info("正在删除id为{}的caipuZhuliao",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的caipuZhuliao成功",id);
            return id;
        } else {
            log.error("删除id为{}的caipuZhuliao失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateCaipuZhuliao(CaipuZhuliao caipuZhuliao) {
        log.info("正在更新id为{}的caipuZhuliao",caipuZhuliao.getCaipuZhuliaoId());
        if (super.updateById(caipuZhuliao)) {
            log.info("更新d为{}的caipuZhuliao成功",caipuZhuliao.getCaipuZhuliaoId());
            return caipuZhuliao.getCaipuZhuliaoId();
        } else {
            log.error("更新id为{}的caipuZhuliao失败",caipuZhuliao.getCaipuZhuliaoId());
            throw new BizException("更新失败[id=" + caipuZhuliao.getCaipuZhuliaoId() + "]");
        }
    }



}
